package com.yohann.algorithm;

/**
 * <p>
 * 线索化二叉树演示
 * </p>
 *
 * @author Yohann
 * @since 2021/1/4 19:40
 */
public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //创建节点
        HeroNode node1 = new HeroNode(1, "松江");
        HeroNode node2 = new HeroNode(3, "吴用");
        HeroNode node3 = new HeroNode(6, "卢俊义");
        HeroNode node4 = new HeroNode(8, "林冲");
        HeroNode node5 = new HeroNode(10, "关胜");
        HeroNode node6 = new HeroNode(14, "马超");

        node1.setLeft(node2);
        node1.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
        threadedBinaryTree.setRoot(node1);
        threadedBinaryTree.threadedNode();

//        System.out.println(node5.getLeft());
//        System.out.println(node5.getRight());

        threadedBinaryTree.displayThreaded();
    }
}

/**
 * 线索化二叉树
 */
class ThreadedBinaryTree {
    private HeroNode root;
    //前驱节点
    private HeroNode pre = null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void threadedNode() {
        this.threadedNode(root);
    }

    //二叉树线索化
    private void threadedNode(HeroNode node) {
        if (node == null) {
            return;
        }

        //索引化左子树
        threadedNode(node.getLeft());

        //索引化当前节点
        if (node.getLeft() == null) {
            node.setLeft(pre);
            node.setLeftType(1);
        }

        //处理后继节点
        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);
            pre.setRightType(1);
        }

        //每处理一个节点后 让当前节点是下一个节点的前驱
        pre = node;

        //索引化右子树
        threadedNode(node.getRight());
    }

    //中序遍历线索化二叉树
    public void displayThreaded() {
        HeroNode node = root;
        while (node != null) {
            while (node.getLeftType() == 0) {
                node = node.getLeft();
            }

            System.out.println(node);

            while (node.getRightType() == 1) {
                node = node.getRight();
                System.out.println(node);
            }

            node = node.getRight();
        }
    }

    //前序遍历
    public void preOrder() {
        if (root != null) {
            root.preOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //中序遍历
    public void midOrder() {
        if (root != null) {
            root.midOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //后序遍历
    public void postOrder() {
        if (root != null) {
            root.postOrder();
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //前序遍历查找
    public void preorderSearch(int no) {
        if (root != null) {
            HeroNode result = root.preorderSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //中序遍历查找
    public void midSearch(int no) {
        if (root != null) {
            HeroNode result = root.midSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //后序遍历查找
    public void postSearch(int no) {
        if (root != null) {
            HeroNode result = root.postSearch(no);

            if (result == null) {
                System.out.println("没有目标节点！");
            } else {
                System.out.println(result);
            }
        } else {
            System.out.println("二叉树为空！");
        }
    }

    //删除节点
    public void delNode(int no) {
        if (root != null) {
            //判断根节点是否是目标节点
            if (root.getNo() == no) {
                root = null;
            } else {
                root.delNode(no);
            }
        } else {
            System.out.println("当前树为空");
        }
    }
}

/**
 * 节点
 */
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    /*
    leftType=0表示左子树，leftType=1表示前继节点
    rightType=0表示左子树，rightType=1表示后继节点
     */
    private int leftType;
    private int rightType;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void preOrder() {
        /*
        1.输出当前节点
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //输出父节点
        System.out.println(this);

        //向左子树递归
        if (this.left != null) {
            this.left.preOrder();
        }

        //向右子树递归
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void midOrder() {
        /*
        1.向左递归
        2.输出当前节点
        3.向右递归
         */

        //向左子树递归
        if (this.left != null) {
            this.left.midOrder();
        }

        //输出当前点
        System.out.println(this);

        //向右子树递归
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        /*
        1.向左递归
        2.向右递归
        3.输出当前节点
         */

        //向左子树递归
        if (this.left != null) {
            this.left.postOrder();
        }

        //向右子树递归
        if (this.right != null) {
            this.right.postOrder();
        }

        //输出当前点
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preorderSearch(int no) {
        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.preorderSearch(no);

            if (result != null) {
                return result;
            }
        }

        //向右子树递归
        if (this.right != null) {
            return this.right.preorderSearch(no);
        }

        return null;
    }

    //中序遍历查找
    public HeroNode midSearch(int no) {

        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */

        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.midSearch(no);

            if (result != null) {
                return result;
            }
        }

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        //向右子树递归
        if (this.right != null) {
            return this.right.midSearch(no);
        }

        return null;
    }

    //后序遍历查找
    public HeroNode postSearch(int no) {

        /*
        1.当前节点是否符合
        2.左子节点不为空？递归
        3.右子节点不为空？递归
         */
        //向左子树递归
        if (this.left != null) {
            HeroNode result = this.left.postSearch(no);

            if (result != null) {
                return result;
            }
        }

        //向右子树递归
        if (this.right != null) {
            HeroNode result = this.right.postSearch(no);

            if (result != null) {
                return result;
            }
        }

        //判断当前节点是否符合
        if (this.no == no) {
            return this;
        }

        return null;
    }

    //删除节点
    public void delNode(int no) {
        //当前节点的左节点为目标节点
        if (this.left != null) {
            if (this.left.no == no) {
                this.left = null;
                return;
            }

            //左子树递归删除
            this.left.delNode(no);
        }

        //当前节点的右节点为目标节点
        if (this.right != null) {
            if (this.right.no == no) {
                this.right = null;
                return;
            }

            //右子树递归删除
            this.right.delNode(no);
        }
    }
}